package com.hethongviet.job.helpers.listadapter;

import com.hethongviet.job.R;
import com.hethongviet.job.helpers.JSONAttributes;
import com.hethongviet.job.models.Task;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ListItemDialogAdapter extends ArrayAdapter<String>
{
	private final String[] values;
	private final Activity activity;

	/* Make higher performance */
	private static class ViewHolder
	{
		public TextView _textDialogItem;
	}

	public ListItemDialogAdapter(Activity activity, String[] values)
	{
		super(activity, R.layout.list_item_dialog, values);
		this.values = values;
		this.activity = activity;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		View rowView = convertView;
		final ViewHolder viewHolder;

		if (rowView == null)
		{
			LayoutInflater layoutInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = layoutInflater.inflate(R.layout.list_item_dialog, parent,
					false);
			viewHolder = new ViewHolder();
			viewHolder._textDialogItem = (TextView) rowView.findViewById(R.id.textDialogItem);
			rowView.setTag(viewHolder);
		}
		else
		{
			viewHolder = (ViewHolder) rowView.getTag();
		}
		/* Initiate items */
		viewHolder._textDialogItem.setText(values[position]);
		viewHolder._textDialogItem.setTextColor(getStatusColor(values[position]));
		/* Return view */
		return rowView;
	}

	/**
	 * the string in localization
	 * 
	 * @param statusString
	 * @return the string response with constant statuses
	 */
	public int getStatusColor(String statusString)
	{
		if (statusString.equalsIgnoreCase(activity.getString(R.string.textTaskStatusDelayFinish))) return Task.getStatusColor(
				activity, JSONAttributes.STATUS_DELAY_FINISH);
		else if (statusString.equalsIgnoreCase(activity.getString(R.string.textTaskStatusDelayStart))) return Task.getStatusColor(
				activity, JSONAttributes.STATUS_DELAY_START);
		else if (statusString.equalsIgnoreCase(activity.getString(R.string.textTaskStatusFinish))) return Task.getStatusColor(
				activity, JSONAttributes.STATUS_FINISH);
		else if (statusString.equalsIgnoreCase(activity.getString(R.string.textTaskStatusInProgress))) return Task.getStatusColor(
				activity, JSONAttributes.STATUS_IN_PROCESS);
		else return Task.getStatusColor(activity,
				JSONAttributes.STATUS_JUST_CREATE);
	}
}
